﻿#include "ThemeManager.h"
#include <QApplication>
#include <QDebug>
#include "Defs.h"
#include "ConfigManager.h"

ThemeManager& ThemeManager::GetInstance() {
    static ThemeManager instance;
    return instance;
}

ThemeManager::ThemeManager() : m_currentTheme(Light) {
    initializeThemes();
    m_currentTheme = ConfigManager::instance().getLastTheme();
    emit themeChanged(m_currentTheme);
}

void ThemeManager::initializeThemes() {
    initializeLightTheme();
    initializeDarkTheme();
}

void ThemeManager::initializeLightTheme() {
    // 主界面
    m_themeData["Light.MainWindow.background"] = "#F8F9FA";

    // 菜单栏
    m_themeData["Light.MenuBar.background"] = "#FFFFFF";
    m_themeData["Light.MenuBar.border"] = "#E1E4E8";
    m_themeData["Light.MenuBar.item.color"] = "#24292E";
    m_themeData["Light.MenuBar.item.hover"] = "#F0F2F5";
    m_themeData["Light.MenuBar.item.pressed"] = "#E1E4E8";

    // 菜单
    m_themeData["Light.Menu.background"] = "#FFFFFF";
    m_themeData["Light.Menu.border"] = "#D1D5DB";
    m_themeData["Light.Menu.item.color"] = "#24292E";
    m_themeData["Light.Menu.item.hover"] = "#F0F2F5";
    m_themeData["Light.Menu.separator"] = "#E1E4E8";

    // 状态栏
    m_themeData["Light.StatusBar.background"] = "#FFFFFF";
    m_themeData["Light.StatusBar.border"] = "#E1E4E8";
    m_themeData["Light.StatusBar.color"] = "#6B7280";
    // 状态栏标签
    m_themeData["Light.StatusBarLabel.color"] = "#24292E";  // 深色文字

    // 中央部件
    m_themeData["Light.CentralWidget.background"] = "#F8F9FA";

    // 分割器
    m_themeData["Light.Splitter.handle"] = "#D1D5DB";
    m_themeData["Light.Splitter.handle.hover"] = "#4A90E2";

    // 侧边栏
    m_themeData["Light.SideBar.background"] = "#FFFFFF";
    m_themeData["Light.SideBar.border"] = "#E1E4E8";

    // 搜索框
    m_themeData["Light.SearchBox.background"] = "#F8F9FA";
    m_themeData["Light.SearchBox.background.hover"] = "#FFFFFF";
    m_themeData["Light.SearchBox.background.focus"] = "#FFFFFF";
    m_themeData["Light.SearchBox.border"] = "#D1D5DB";
    m_themeData["Light.SearchBox.border.hover"] = "#9CA3AF";
    m_themeData["Light.SearchBox.border.focus"] = "#4A90E2";
    m_themeData["Light.SearchBox.color"] = "#24292E";
    m_themeData["Light.SearchBox.placeholder"] = "#6B7280";

    // 主要按钮
    m_themeData["Light.PrimaryButton.background"] = "#4A90E2";
    m_themeData["Light.PrimaryButton.background.hover"] = "#357ABD";
    m_themeData["Light.PrimaryButton.background.pressed"] = "#2E6DA4";
    m_themeData["Light.PrimaryButton.background.disabled"] = "#9BBFE5";
    m_themeData["Light.PrimaryButton.color"] = "white";

    // 次要按钮
    m_themeData["Light.SecondaryButton.background"] = "#F8F9FA";
    m_themeData["Light.SecondaryButton.background.hover"] = "#F3F4F6";
    m_themeData["Light.SecondaryButton.background.pressed"] = "#E5E7EB";
    m_themeData["Light.SecondaryButton.background.disabled"] = "#FAFAFA";
    m_themeData["Light.SecondaryButton.border"] = "#D1D5DB";
    m_themeData["Light.SecondaryButton.border.hover"] = "#9CA3AF";
    m_themeData["Light.SecondaryButton.border.disabled"] = "#E5E7EB";
    m_themeData["Light.SecondaryButton.color"] = "#6B7280";
    m_themeData["Light.SecondaryButton.color.hover"] = "#374151";
    m_themeData["Light.SecondaryButton.color.disabled"] = "#D1D5DB";

    // 编辑器
    //m_themeData["Light.CodeEditor.background"] = "#FFFFFF";
    m_themeData["Light.CodeEditor.border"] = "#E1E4E8";

    // 圆角半径
    m_themeData["Light.border.radius.small"] = "6px";
    m_themeData["Light.border.radius.medium"] = "8px";
    m_themeData["Light.border.radius.large"] = "8px";

    // 字体大小
    m_themeData["Light.font.size.normal"] = "14px";
    m_themeData["Light.font.size.large"] = "18px";
    m_themeData["Light.font.size.xlarge"] = "24px";
}

void ThemeManager::initializeDarkTheme() {
    // 主界面
    m_themeData["Dark.MainWindow.background"] = "#1E1E1E";

    // 菜单栏
    m_themeData["Dark.MenuBar.background"] = "#2D2D30";
    m_themeData["Dark.MenuBar.border"] = "#3E3E42";
    m_themeData["Dark.MenuBar.item.color"] = "#E8E8E8";   // 浅色文字
    m_themeData["Dark.MenuBar.item.hover"] = "#3E3E42";
    m_themeData["Dark.MenuBar.item.pressed"] = "#4E4E52";

    // 菜单
    m_themeData["Dark.Menu.background"] = "#2D2D30";
    m_themeData["Dark.Menu.border"] = "#3E3E42";
    m_themeData["Dark.Menu.item.color"] = "#E8E8E8";      // 浅色文字
    m_themeData["Dark.Menu.item.hover"] = "#3E3E42";
    m_themeData["Dark.Menu.separator"] = "#3E3E42";

    // 状态栏
    m_themeData["Dark.StatusBar.background"] = "#2D2D30";
    m_themeData["Dark.StatusBar.border"] = "#3E3E42";
    m_themeData["Dark.StatusBar.color"] = "#CCCCCC";      // 浅灰色文字
    // 状态栏标签
    m_themeData["Dark.StatusBarLabel.color"] = "#E8E8E8";  // 浅色文字

    // 中央部件
    m_themeData["Dark.CentralWidget.background"] = "#1E1E1E";

    // 分割器
    m_themeData["Dark.Splitter.handle"] = "#3E3E42";
    m_themeData["Dark.Splitter.handle.hover"] = "#4A90E2";

    // 侧边栏
    m_themeData["Dark.SideBar.background"] = "#252526";
    m_themeData["Dark.SideBar.border"] = "#3E3E42";

    // 搜索框
    m_themeData["Dark.SearchBox.background"] = "#3C3C3C";
    m_themeData["Dark.SearchBox.background.hover"] = "#464647";
    m_themeData["Dark.SearchBox.background.focus"] = "#464647";
    m_themeData["Dark.SearchBox.border"] = "#464647";
    m_themeData["Dark.SearchBox.border.hover"] = "#5A5A5A";
    m_themeData["Dark.SearchBox.border.focus"] = "#4A90E2";
    m_themeData["Dark.SearchBox.color"] = "#E8E8E8";      // 浅色文字
    m_themeData["Dark.SearchBox.placeholder"] = "#A0A0A0"; // 浅灰色占位符

    // 主要按钮
    m_themeData["Dark.PrimaryButton.background"] = "#4A90E2";
    m_themeData["Dark.PrimaryButton.background.hover"] = "#357ABD";
    m_themeData["Dark.PrimaryButton.background.pressed"] = "#2E6DA4";
    m_themeData["Dark.PrimaryButton.background.disabled"] = "#2A5A8A";
    m_themeData["Dark.PrimaryButton.color"] = "white";    // 白色文字

    // 次要按钮
    m_themeData["Dark.SecondaryButton.background"] = "#3C3C3C";
    m_themeData["Dark.SecondaryButton.background.hover"] = "#464647";
    m_themeData["Dark.SecondaryButton.background.pressed"] = "#4E4E52";
    m_themeData["Dark.SecondaryButton.background.disabled"] = "#2D2D30";
    m_themeData["Dark.SecondaryButton.border"] = "#5A5A5A";
    m_themeData["Dark.SecondaryButton.border.hover"] = "#6B6B6B";
    m_themeData["Dark.SecondaryButton.border.disabled"] = "#3E3E42";
    m_themeData["Dark.SecondaryButton.color"] = "#CCCCCC";        // 浅灰色文字
    m_themeData["Dark.SecondaryButton.color.hover"] = "#E8E8E8";  // 更浅的文字
    m_themeData["Dark.SecondaryButton.color.disabled"] = "#7A7A7A";

    // 编辑器
    //m_themeData["Dark.CodeEditor.background"] = "#1E1E1E";
    m_themeData["Dark.CodeEditor.border"] = "#3E3E42";

    // 圆角半径
    m_themeData["Dark.border.radius.small"] = "6px";
    m_themeData["Dark.border.radius.medium"] = "8px";
    m_themeData["Dark.border.radius.large"] = "8px";

    // 字体大小
    m_themeData["Dark.font.size.normal"] = "14px";
    m_themeData["Dark.font.size.large"] = "18px";
    m_themeData["Dark.font.size.xlarge"] = "24px";
}

void ThemeManager::setTheme(Theme theme) {
    if (m_currentTheme != theme) {
        m_currentTheme = theme;

        ConfigManager::instance().setLastTheme(theme);

		emit themeChanged(m_currentTheme);
        qDebug() << "主题已切换为:" << themeName();
    }
}

QColor ThemeManager::getColor(const QString& key) const {
    QString themeKey = QString("%1.%2").arg(themeName()).arg(key);
    if (m_themeData.contains(themeKey)) {
        return QColor(m_themeData[themeKey].toString());
    }
    return QColor("#000000"); // 默认返回黑色
}

QString ThemeManager::getValue(const QString& key) const {
    QString themeKey = QString("%1.%2").arg(themeName()).arg(key);
    if (m_themeData.contains(themeKey)) {
        return m_themeData[themeKey].toString();
    }
    return "";
}

QString ThemeManager::getMainWindowStyle() const {
    return QString(R"(
        QMainWindow {
            background-color: %1;
        }
    )").arg(getColor("MainWindow.background").name());
}

QString ThemeManager::getMenuBarStyle() const {
    return QString(R"(
        QMenuBar {
            background-color: %1;
            border-bottom: 1px solid %2;
            padding: 2px 0px;
        }
        QMenuBar::item {
            background-color: transparent;
            padding: 6px 12px;
            color: %3;
            border-radius: %4;
        }
        QMenuBar::item:selected {
            background-color: %5;
        }
        QMenuBar::item:pressed {
            background-color: %6;
        }
    )").arg(
    getColor("MenuBar.background").name(),
    getColor("MenuBar.border").name(),
    getColor("MenuBar.item.color").name(),
    getValue("border.radius.small"),
    getColor("MenuBar.item.hover").name(),
    getColor("MenuBar.item.pressed").name()
);
}

QString ThemeManager::getMenuStyle() const {
    return QString(R"(
        QMenu {
            background-color: %1;
            border: 1px solid %2;
            border-radius: %3;
            padding: 4px;
        }
        QMenu::item {
            padding: 8px 24px 8px 12px;
            border-radius: %4;
            color: %5;
        }
        QMenu::item:selected {
            background-color: %6;
        }
        QMenu::separator {
            height: 1px;
            background-color: %7;
            margin: 4px 8px;
        }
    )").arg(
    getColor("Menu.background").name(),
    getColor("Menu.border").name(),
    getValue("border.radius.small"),
    getValue("border.radius.small"),
    getColor("Menu.item.color").name(),
    getColor("Menu.item.hover").name(),
    getColor("Menu.separator").name()
);
}

QString ThemeManager::getStatusBarStyle() const {
    return QString(R"(
        QStatusBar {
            background-color: %1;
            border-top: 1px solid %2;
            color: %3;
        }
        QStatusBar QLabel {
            color: %3;
            background: transparent;
            border: none;
            padding: 0px;
            margin: 0px;
        }
        QStatusBar QPushButton {
            color: %3;
            background: transparent;
            border: none;
            padding: 0px;
            margin: 0px;
        }
    )").arg(
    getColor("StatusBar.background").name(),
    getColor("StatusBar.border").name(),
    getColor("StatusBar.color").name()
);
}

QString ThemeManager::getSplitterStyle() const {
    return QString(R"(
        QSplitter::handle {
            background-color: %1;
            width: 1px;
            margin: 0px 4px;
        }
        QSplitter::handle:hover {
            background-color: %2;
        }
    )").arg(
    getColor("Splitter.handle").name(),
    getColor("Splitter.handle.hover").name()
);
}

QString ThemeManager::getSideBarStyle() const {
    return QString(R"(
        QWidget#recentBar, QWidget#labelBar {
            background-color: %1;
            border: 1px solid %2;
            border-radius: %3;
        }
    )").arg(
    getColor("SideBar.background").name(),
    getColor("SideBar.border").name(),
    getValue("border.radius.large")
);
}

QString ThemeManager::getSearchBoxStyle() const {
    QString temp = QString(R"(
        QLineEdit {
            background-color: %1;
            border: 1.5px solid %2;
            padding: 10px 14px;
            border-radius: %3;
            font-size: %4;
            color: %5;
            selection-background-color: #4A90E2;
            selection-color: #FFFFFF;
        }
        QLineEdit:hover {
            border: 1.5px solid %6;
            background-color: %7;
        }
        QLineEdit:focus {
            border: 2px solid %8;
            background-color: %9;
            outline: none;
            padding: 9.5px 13.5px;
        }
        QLineEdit:disabled {
            background-color: %10;
            color: %11;
            border: 1.5px solid %12;
        }
    )");

    temp = temp.arg(getColor("SearchBox.background").name())
            .arg(getColor("SearchBox.border").name())
            .arg(getValue("border.radius.small"))
            .arg(getValue("font.size.normal"))
            .arg(getColor("SearchBox.color").name())
            .arg(getColor("SearchBox.border.hover").name())
            .arg(getColor("SearchBox.background.hover").name())
            .arg(getColor("SearchBox.border.focus").name())
            .arg(getColor("SearchBox.background.focus").name())
            .arg( getColor("SecondaryButton.background.disabled").name())
            .arg(getColor("SecondaryButton.color.disabled").name())
            .arg(getColor("SecondaryButton.border.disabled").name());
    return temp;
}

QString ThemeManager::getPrimaryButtonStyle() const {
    return QString(R"(
        QPushButton {
            background-color: %1;
            color: %2;
            border: none;
            border-radius: %3;
            padding: 0px;
            font-size: %4;
            font-weight: 500;
        }
        QPushButton:hover {
            background-color: %5;
        }
        QPushButton:pressed {
            background-color: %6;
        }
        QPushButton:disabled {
            background-color: %7;
        }
    )").arg(
    getColor("PrimaryButton.background").name(),
    getColor("PrimaryButton.color").name(),
    getValue("border.radius.small"),
    getValue("font.size.large"),
    getColor("PrimaryButton.background.hover").name(),
    getColor("PrimaryButton.background.pressed").name(),
    getColor("PrimaryButton.background.disabled").name()
);
}


QString ThemeManager::getSecondaryButtonStyle() const {
    QString temp = QString(R"(
        QPushButton {
            background-color: %1;
            color: %2;
            border: 1.5px solid %3;
            border-radius: %4px;  /* 补充px单位 */
            padding: 0px;
            font-size: %5px;     /* 补充px单位 */
            font-weight: bold;
        }
        QPushButton:hover {
            background-color: %6;
            border: 1.5px solid %7;
            color: %8;
        }
        QPushButton:pressed {
            background-color: %9;
        }
        QPushButton:disabled {
            background-color: %10;
            color: %11;
            border: 1.5px solid %12;
        }
    )");

    // 链式调用arg()，确保参数顺序与占位符严格对应
    temp = temp.arg(getColor("SecondaryButton.background").name())
        .arg(getColor("SecondaryButton.color").name())
        .arg(getColor("SecondaryButton.border").name())
        // 处理数值参数，确保非空（若主题数据缺失则用默认值）
        .arg(getValue("border.radius.small").isEmpty() ? "4" : getValue("border.radius.small"))
        .arg(getValue("font.size.xlarge").isEmpty() ? "16" : getValue("font.size.xlarge"))
        .arg(getColor("SecondaryButton.background.hover").name())
        .arg(getColor("SecondaryButton.border.hover").name())
        .arg(getColor("SecondaryButton.color.hover").name())
        .arg(getColor("SecondaryButton.background.pressed").name())
        .arg(getColor("SecondaryButton.background.disabled").name())
        .arg(getColor("SecondaryButton.color.disabled").name())
        .arg(getColor("SecondaryButton.border.disabled").name());

    return temp;
}


QString ThemeManager::getCodeEditorStyle() const {
    /*return QString(R"(
        QPlainTextEdit {
            background-color: %1;
            border: 1px solid %2;
            border-radius: %3;
            padding: 8px;
        }
    )").arg(
    getColor("CodeEditor.background").name(),
    getColor("CodeEditor.border").name(),
    getValue("border.radius.large")
    );*/

    return "";
}

void ThemeManager::setCustomColor(const QString& key, const QColor& color) {
    QString themeKey = QString("%1.%2").arg(themeName()).arg(key);
    m_themeData[themeKey] = color.name();
}

void ThemeManager::setCustomValue(const QString& key, const QVariant& value) {
    QString themeKey = QString("%1.%2").arg(themeName()).arg(key);
    m_themeData[themeKey] = value;
}

QString ThemeManager::themeName() const {
    switch (m_currentTheme) {
    case Light: return "Light";
    case Dark: return "Dark";
    default: return "Light";
    }
}